#define _CRT_SECURE_NO_WARNINGS
#include "Stack.h"

void STInit(STNode* stnode)
{
	assert(stnode);
	stnode->stdata = NULL;
	stnode->size = 0;
	stnode->capacity = 0;
}

void STDestroy(STNode* stnode)
{
	assert(stnode);
	assert(stnode->stdata);
	free(stnode->stdata); 
	stnode->stdata = NULL;
	stnode->size = 0;
	stnode->capacity = 0;
}

void STPush(STNode* stnode, StackDataType x)
{
	assert(stnode);
	if (stnode->size == stnode->capacity)
	{
		int newsize = stnode->size == 0 ? 4 : stnode->size * 2;
		StackDataType* tmp = (StackDataType*)(realloc(stnode->stdata, sizeof(StackDataType)*newsize));
		if (tmp == NULL)
		{
			perror("relloc Fail");
			return;
		}
		stnode->stdata = tmp;
		stnode->capacity = newsize;
	}
	stnode->stdata[stnode->size++] = x;
}

StackDataType STPop(STNode* stnode)
{
	assert(stnode);
	assert(!STEmpty(stnode));
	StackDataType data = stnode->stdata[--stnode->size];
	return data;
}

bool STEmpty(STNode* stnode)
{
	assert(stnode);
	return stnode->size == 0;
}